package com.wit.witmg.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wit.witmg.entity.TProdmater;
import com.wit.witmg.entity.TProdtech;
import com.wit.witmg.mapper.TProdtechMapper;
import com.wit.witmg.service.ITProdtechService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wit.witmg.util.ErrorCode;
import com.wit.witmg.util.JsonResult;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bob
 * @since 2024-12-24
 */
@Service
public class TProdtechServiceImpl extends ServiceImpl<TProdtechMapper, TProdtech> implements ITProdtechService {

    @Override
    public Object add(HttpServletRequest request, String json) {
        TProdtech t= JSON.parseObject(json,TProdtech.class);
        if(t.getId()>0){
            boolean bl=  updateById(t);
            if(!bl){
                return JsonResult.error(ErrorCode.ERR_200);
            }
        }else{
            t.setDate(new Date());
            boolean bl= save(t);
            if(!bl){
                return JsonResult.error(ErrorCode.ERR_504);
            }
        }
        return JsonResult.success(t);
    }

    @Override
    public Object get(HttpServletRequest request, Integer id, String pid, String bn) {
        QueryWrapper<TProdtech> qw=new QueryWrapper<>();
        if(id!=null){
            return JsonResult.success(getById(id));
        }
        if( pid!=null){//pid模糊搜索pid包含在参数内的数据
            return JsonResult.success(baseMapper.findByBnContaining(pid));
        }
        if(bn!=null){
            qw.like("bn",bn);
            return JsonResult.success(list(qw));
        }
        return JsonResult.error(ErrorCode.ERR_200);
    }

    @Override
    public Object del(HttpServletRequest request, Integer id) {
        boolean bl= removeById(id);
        if(bl) {
            return JsonResult.success();
        }
        return JsonResult.error(ErrorCode.ERR_200);
    }
}
